package vg.com.frame;

import com.kdstudio.network.URLCachePolicy;
import com.kdstudio.network.URLRequest;
import com.kdstudio.network.URLRequestDelegate;
import com.kdstudio.network.reponse.URLBitmapResponse;

import vg.com.chatonline.activity.R;
import vg.com.frame.friend_frame.httpBitmapRequestDelegate;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class img_1_3 extends LinearLayout{
	
	ImageView img;
	private Handler mHanlder;
	
	public img_1_3(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}
	
	public img_1_3(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		String servie=Context.LAYOUT_INFLATER_SERVICE;
		LayoutInflater inflate=(LayoutInflater)context.getSystemService(servie);
		View v=inflate.inflate(R.layout.img_height_1_3, null);
		setImg((ImageView)v.findViewById(R.id.img_1_3));
		int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
		this.addView(v,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, screenWidth/2));
	}
	
	public void fillData(String url)
	{
		mHanlder=new Handler();
		//khoi tao request gui lenh den server
		URLRequest request=new URLRequest(url,new httpBitmapRequestDelegate());
		//gui va lay theo kieu gi(Json hay BitMap)
		request.setReponse(new URLBitmapResponse());
		//set che do cache cua request
		request.setCachePolicy(URLCachePolicy.LOCAL);
		request.send();
		getImg().setBackgroundResource(R.drawable.no_avatar);
	}

	public ImageView getImg() {
		return img;
	}

	public void setImg(ImageView img) {
		this.img = img;
	}
	
	public class httpBitmapRequestDelegate extends URLRequestDelegate
	{
		public void onRequestReponse(URLRequest httpRequest) {
			// TODO Auto-generated method stub
			//kieu can response(Json hay BitMap)
			final URLBitmapResponse response=(URLBitmapResponse)httpRequest.getReponse();
			mHanlder.post(new Runnable() {
				
				public void run() {
					// TODO Auto-generated method stub
					//set anh
					getImg().setImageBitmap(response.bitmap);
				}
			});
			
			super.onRequestReponse(httpRequest);
		}
	}

	
}
